package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.SelectModel;
import org.testis.gui.mvc.view.testdesign.SelectView;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import org.testis.orm.classes.T_dmap_designtree;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestPlanRefined;
import org.testis.utils.middleware.DynamicMap;
import org.testis.utils.middleware.TestDesign;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class SelectController extends AbstractController {

    public static final String TREE_LOAD = "LoadTree";
    public static final String TREE_REFRESH = "RefreshTree";
    public static final String CHANGE_TITLE = "Title";
    public static final String SELECT_DESIGN_PART = "SelectedNode";
    public static final String SHOW_SELECTER = "ShowSelecter";
    public static final String ROOT = "Root";
    public static final String EXPAND_PATH = "ExpandPath";
    public static final String PROPERTIES = "Properties";
    public static final String CHANGE_SELECTED_NO = "SelectedNumber";
    private int selectedNo;
    private Selectable m_selectable;
    private boolean addChildMode;
    private int m_listSize;
    private TreeNode m_clickedNode;
    private String m_clickedNodePath;

    public SelectController() {
        super();
        m_listSize = 0;
    }

    public void operationChangeSize(int listSize) {
        m_listSize = listSize;
    }

    public void operationChangeTitle(String title) {
        setModelProperty(CHANGE_TITLE,
                title);
    }

    public void operationCheckboxClicked(MyDefaultMutableTreeNode node, boolean isSelected) {
        boolean checkBoxSelectable = true;
        MyDefaultMutableTreeNode child;
        if (addChildMode && node.getParent() != null) {
            if (!node.isCheckBoxSelectable()) {
                return;
            }
            for (int i = 0; i < node.getParent().getChildCount(); i++) {
                child = node.getParent().getChildAt(i);
                checkBoxSelectable = checkBoxSelectable && child.isCheckBoxSelectable();
                child.setCheckBoxSelectable(true);
                if (child.isSelected()) {
                    child.setSelectedSingle(false);
                    decrementSelectedNumber();
                }
            }
            node.setCheckBoxSelectable(checkBoxSelectable);
            if (isSelected) {
                incrementSelectedNumber();
            }
            node.setSelectedSingle(isSelected);
        } else {
            if (isSelected) {
                incrementSelectedNumber();
            } else {
                decrementSelectedNumber();
            }
            node.setSelectedSingle(isSelected);
        }
    }

    public void showAll(boolean selected) {
        if (selected) {
            selectAll(false);
        }
        MyDefaultMutableTreeNode temp;
        Iterator<Integer> it = nodesIndex.keySet().iterator();
        int nextValue;
        while (it.hasNext()) {
            nextValue = it.next().intValue();
            temp = nodesIndex.get(nextValue);
            temp.setHasCheckBox(true);
        }
        if (selected) {
            selectAll(true);
        }
        operationRefreshTree();
    }

    private boolean haveSamePathWithClickedNode(TreeNode temp) {
        String path = "";
        if (temp instanceof TestDesignTreeNode) {
            TestDesignTreeNode clickedNode = (TestDesignTreeNode) m_clickedNode;
            TestDesignTreeNode node = (TestDesignTreeNode) temp;
            if (!node.isClone()) {
                return true;
            } else if (node.getId() == clickedNode.getId()) {
                return true;
            }
            TestDesignTreeNode parent = node;
            while (!(parent.getObject() instanceof TestPlanRefined)) {
                path += parent.getReference().getOriginalTreeReference().getId() + "-";
                parent = parent.getParent();
                if (parent == null) {
                    break;
                }
            }
            path += temp.getPlace();

            if (m_clickedNodePath.equals("")) {
                parent = clickedNode;
                while (!(parent.getObject() instanceof TestPlanRefined)) {
                    m_clickedNodePath += parent.getReference().getOriginalTreeReference().getId() + "-";
                    parent = parent.getParent();
                    if (parent == null) {
                        break;
                    }
                }
                m_clickedNodePath += clickedNode.getPlace();
            }
            System.out.println(path + "  " + m_clickedNodePath);
        } else if (temp instanceof DynamicMapDesignTreeNode) {
            DynamicMapDesignTreeNode node = (DynamicMapDesignTreeNode) temp;
            DynamicMapDesignTreeNode clickedNode = (DynamicMapDesignTreeNode) m_clickedNode;
            if (!node.isClone()) {
                return true;
            } else if (node.getId() == clickedNode.getId()) {
                return true;
            }
            DynamicMapDesignTreeNode parent = node;
            while (!(parent.getObject().getTypeName().equals("UIDeviceMapContainer"))) {
                path += parent.getObject().getId() + "-";
                parent = parent.getParent();
                if (parent == null) {
                    break;
                } else if (parent.getObject().getTypeName().equals("UIDeviceMapContainer")) {
                    break;
                }
            }
            path += temp.getPlace();
            if (m_clickedNodePath.equals("")) {
                parent = clickedNode;
                while (!(parent.getObject().getTypeName().equals("UIDeviceMapContainer"))) {
                    m_clickedNodePath += parent.getObject().getId() + "-";
                    parent = parent.getParent();
                    if (parent == null) {
                        break;
                    } else if (parent.getObject().getTypeName().equals("UIDeviceMapContainer")) {
                        break;
                    }
                }

                m_clickedNodePath += clickedNode.getPlace();
            }
        }
        if (m_clickedNodePath.equals(path)) {
            return true;
        }
        return false;

    }

    public void showSameParents(boolean selected) {
        if (selected) {
            selectAll(false);
        }
        MyDefaultMutableTreeNode temp;
        Iterator<Integer> it = nodesIndex.keySet().iterator();
        int nextValue;
        while (it.hasNext()) {
            nextValue = it.next().intValue();
            temp = nodesIndex.get(nextValue);
            if (addChildMode) {
                if (haveSamePathWithClickedNode((TreeNode) temp.getParent().getUserObject())) {
                    temp.setHasCheckBox(true);
                } else {
                    temp.setHasCheckBox(false);
                }
            } else {
                if (haveSamePathWithClickedNode((TreeNode) temp.getUserObject())) {
                    temp.setHasCheckBox(true);
                } else {
                    temp.setHasCheckBox(false);

                }
            }
        }
        if (selected) {
            selectAll(true);
        }
        operationRefreshTree();
    }

    private void operationChangeSelectedNumber(String number) {
        setModelProperty(CHANGE_SELECTED_NO,
                number + " / " + m_listSize);
    }

    public void incrementSelectedNumber() {
        selectedNo++;
        operationChangeSelectedNumber("" + selectedNo);
    }

    public void decrementSelectedNumber() {
        selectedNo--;
        operationChangeSelectedNumber("" + selectedNo);
    }

    public void operationChangeProperties(String properties) {
        setModelProperty(PROPERTIES,
                properties);
    }

    public void selectAll(boolean selected) {

        if (!addChildMode) {
            Iterator<Integer> it = nodesIndex.keySet().iterator();
            int nextValue;
            int selectedCount = 0;
            while (it.hasNext()) {
                nextValue = it.next().intValue();
                if (nodesIndex.get(nextValue).isCheckBoxSelectable() && nodesIndex.get(nextValue).hasCheckBox()) {
                    nodesIndex.get(nextValue).setSelectedSingle(selected);
                } else if (!nodesIndex.get(nextValue).hasCheckBox()) {
                    nodesIndex.get(nextValue).setSelectedSingle(false);
                }
                if (nodesIndex.get(nextValue).isSelected()) {
                    selectedCount++;
                }
            }
            selectedNo = selectedCount;
            operationChangeSelectedNumber("" + selectedNo);
        } else {
           MyDefaultMutableTreeNode temp;
            Iterator<Integer> it = nodesIndex.keySet().iterator();
            int nextValue;
            int count=1;
            while (it.hasNext()) {
                nextValue = it.next().intValue();
                temp = nodesIndex.get(nextValue);
                boolean isParentHasUnSelectableNode=false;
                if(temp.toString().isEmpty()){
                    int children=temp.getParent().getChildCount();
                    for(int i=0;i<children;i++){
                        if(!temp.getParent().getChildAt(i).isCheckBoxSelectable()){
                            isParentHasUnSelectableNode=true;
                            break;
                        }
                    }
                    if(!isParentHasUnSelectableNode)
                    {
                        temp.setSelectedSingle(selected);
                        if(selected)
                             count++;
                    }
                }else if(temp.isCheckBoxSelectable()){
                   temp.setSelectedSingle(false);
                }
            }
 
            selectedNo = count;
            operationChangeSelectedNumber("" + selectedNo);
        }
        operationRefreshTree();
    }

    public void operationClickOk() {
        LinkedList<MyDefaultMutableTreeNode> selectList = new LinkedList<MyDefaultMutableTreeNode>();
        LinkedList<MyDefaultMutableTreeNode> unselectList = new LinkedList<MyDefaultMutableTreeNode>();
        Iterator<Integer> it = nodesIndex.keySet().iterator();
        while (it.hasNext()) {
            int addition = it.next();
            if (nodesIndex.get(addition).isSelected()) {
                selectList.add(nodesIndex.get(addition));
            } else {
                unselectList.add(nodesIndex.get(addition));
            }
        }
        System.out.println("NO OF SELECTED =" + selectList.size());
        System.out.println("NO OF NOT SELECTED =" + unselectList.size());
        if (selectList.size() > 0) {
            m_selectable.setSelected(selectList.toArray(new MyDefaultMutableTreeNode[selectList.size()]));
        }
        if (unselectList.size() > 0) {
            m_selectable.setNotSelected(unselectList.toArray(new MyDefaultMutableTreeNode[unselectList.size()]));
        }
    }

    public void operationCloseWindow() {
        cleanAll();
    }

    private void initialize() {
        MyDefaultMutableTreeNode root = new MyDefaultMutableTreeNode(false);
        SelectView view = new SelectView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                root, this);
        SelectModel model = new SelectModel();
        addView(view);
        addModel(model);
        operationSetRoot(root);
        nodesIndex.clear();
        view.pack();
    }

    public void operationSetRoot(MyDefaultMutableTreeNode root) {
        setModelProperty(ROOT,
                root);
    }

    public MyDefaultMutableTreeNode operationGetRoot() {
        return (MyDefaultMutableTreeNode) getModelProperty(ROOT);
    }

    public void operationRefreshTree() {
        setModelProperty(TREE_REFRESH,
                true);
    }

    synchronized public void operationLoadTree(MyDefaultMutableTreeNode tree) {
        setModelProperty(TREE_LOAD,
                tree);
    }

    synchronized public void operationSelectDesignPart(MyDefaultMutableTreeNode node) {
        if (node != null && node.getUserObject() != null) {
            setModelProperty(SELECT_DESIGN_PART,
                    node);
            operationChangeProperties(node.getUserObject().toString());
        }
    }

    synchronized public void operationSelectDesignPart(Integer node) {
        if (node == null) {
            return;
        }
        MyDefaultMutableTreeNode root =
                (MyDefaultMutableTreeNode) getModelProperty(ROOT);
        if (root.getChildCount() <= node) {
            return;
        }
        setModelProperty(SELECT_DESIGN_PART,
                root.getChildAt(node));
    }

    public void startTestDesignSelecter(Selectable selectable, LinkedList<TestDesignTreeNode> list,
            Integer[] defaultSelected, TestDesignTreeNode clickedNode,
            String title,
            boolean addchild) {
        LinkedList<MyDefaultMutableTreeNode> defaultSelectedTreeNodes = new LinkedList<MyDefaultMutableTreeNode>();
        if (list != null && list.size() > 0) {
            m_selectable = selectable;
            while (!(this.getNoOfViews() == 0 && this.getNoOfModels() == 0)) {
                SelectController newController = new SelectController();
                newController.startTestDesignSelecter(selectable,
                        list,
                        defaultSelected, clickedNode,
                        title,
                        addchild);
                TestDesignControllerList.newInstance().setSelectController(newController);
                return;
            }
            m_clickedNode = clickedNode;
            m_clickedNodePath = "";
            TestDesignControllerList.newInstance().getSelectController().operationChangeSize(list.size());
            initialize();
            operationChangeTitle(title);
            HashMap<Integer, LinkedList> nodeMap = new HashMap<Integer, LinkedList>();
            T_test_designtree temp;
            for (int i = 0; i < list.size(); i++) {
                temp = list.get(i).getReference().getTreeReference();
                while (temp.getParent() != null) {
                    temp = temp.getParent();
                }
                TestDesignTreeNode suite = TestDesignTreeNode.getNode(TestDesignTreeRefined.getDesignTree(temp));
                addIfUnique(suite, nodeMap);
                nodeMap.get(suite.getId()).add(list.get(i).getReference().getTreeReference());
            }
            Iterator it = nodeMap.keySet().iterator();
            TestDesignTreeNode root = null;
            addChildMode = addchild;
            LinkedList<MyDefaultMutableTreeNode> treeList = new LinkedList<MyDefaultMutableTreeNode>();
            while (it.hasNext()) {
                root = TestDesign.newInstance().getFlyWeightTestDesignTree(nodeMap.get(it.next()));
                if (root != null) {
                    MyDefaultMutableTreeNode nodeToLoad = constructTree(root, false, list);
                    if (nodeToLoad.getColor() == MyDefaultMutableTreeNode.DIFF_NAME) {
                        treeList.add(0, nodeToLoad);
                    } else {
                        treeList.add(nodeToLoad);
                    }
                    nodeToLoad.setColor(MyDefaultMutableTreeNode.DEFAULT);
                }
            }
            for (int i = 0; i < treeList.size(); i++) {
                operationLoadTree(treeList.get(i));
                // System.out.println("TREE LOADED for "+treeList.get(i).getName());
            }
            if (defaultSelected != null && defaultSelected.length > 0) {
                for (int i = 0; i < defaultSelected.length; i++) {
                    if (nodesIndex.get(defaultSelected[i].intValue()) != null) {
                        defaultSelectedTreeNodes.add(nodesIndex.get(defaultSelected[i].intValue()));
                    }
                }
            }
            if (addchild) {
                nodesIndex.clear();
                convertToAddTree(operationGetRoot(), false);
            }
            for (int i = 0; i < defaultSelectedTreeNodes.size(); i++) {
                MyDefaultMutableTreeNode nodeToSelect = defaultSelectedTreeNodes.get(i);
                if (addchild) {
                    if (nodeToSelect != null && nodeToSelect.getChildCount() > 0) {
                        nodeToSelect.getChildAt(nodeToSelect.getChildCount() - 1).setCheckBoxSelectable(false);
                        nodeToSelect.getChildAt(nodeToSelect.getChildCount() - 1).setSelectedSingle(true);
                    }
                } else {
                    nodeToSelect.setCheckBoxSelectable(false);
                    nodeToSelect.setSelectedSingle(true);
                }
            }
            operationRefreshTree();
            Iterator<Integer> ite = nodesIndex.keySet().iterator();
            while (ite.hasNext()) {
                operationExpandPath(nodesIndex.get(ite.next().intValue()));
            }
            selectedNo = defaultSelected.length;
            operationChangeSelectedNumber("" + selectedNo);
            setModelProperty(SHOW_SELECTER,
                    true);
        }
    }

    private void convertToAddTree(MyDefaultMutableTreeNode node, boolean hasCheckBox) {
        if (node == null) {
            throw new NullPointerException("NULL POINTER PARAMETER GIVEN");
        }

        for (int i = 0; i < node.getChildCount(); i++) {
            convertToAddTree(node.getChildAt(i), node.hasCheckBox());
        }
        if (node.hasCheckBox()) {
            node.setSelectedSingle(false);
            MyDefaultMutableTreeNode newEmptyNode = new MyDefaultMutableTreeNode("", true);
            if (node.getChildCount() > 0) {
                node.insert(newEmptyNode, 0);
            } else {
                node.add(newEmptyNode);
            }
            //newEmptyNode.setSelectedSingle(true);
            nodesIndex.put(nodesIndex.size(), newEmptyNode);
        }
        node.setHasCheckBox(hasCheckBox);
        if (hasCheckBox) {
            nodesIndex.put(nodesIndex.size(), node);
        }
    }

    private void operationExpandPath(MyDefaultMutableTreeNode node) {
        setModelProperty(EXPAND_PATH,
                node);
    }

    public void startDynamicMapSelecter(Selectable selectable, LinkedList<DynamicMapDesignTreeNode> list,
            Integer[] defaultSelected, DynamicMapDesignTreeNode clickedNode,
            String title,
            boolean addchild) {
        LinkedList<MyDefaultMutableTreeNode> defaultSelectedTreeNodes = new LinkedList<MyDefaultMutableTreeNode>();
        if (list != null && list.size() > 0) {
            m_selectable = selectable;
            while (!(this.getNoOfViews() == 0 && this.getNoOfModels() == 0)) {
                SelectController newController = new SelectController();
                TestDesignControllerList.newInstance().setSelectController(newController);
                newController.startDynamicMapSelecter(selectable,
                        list,
                        defaultSelected, clickedNode,
                        title,
                        addchild);
                return;
            }
            m_clickedNode = clickedNode;
            m_clickedNodePath = "";
            TestDesignControllerList.newInstance().getSelectController().operationChangeSize(list.size());
            initialize();
            operationChangeTitle(title);
            HashMap<Integer, LinkedList> nodeMap = new HashMap<Integer, LinkedList>();
            T_dmap_designtree temp;
            for (int i = 0; i < list.size(); i++) {
                temp = list.get(i).getReference();
                while (temp.getParent() != null) {
                    temp = temp.getParent();
                }
                DynamicMapDesignTreeNode suite = DynamicMapDesignTreeNode.getNode(temp);
                addIfUnique(suite, nodeMap);
                nodeMap.get(suite.getId()).add(list.get(i).getReference());
            }
            Iterator it = nodeMap.keySet().iterator();
            DynamicMapDesignTreeNode root = null;
            addChildMode = addchild;
            LinkedList<MyDefaultMutableTreeNode> treeList = new LinkedList<MyDefaultMutableTreeNode>();
            while (it.hasNext()) {
                root = DynamicMap.newInstance().getFlyWeightDynamicMapTree(nodeMap.get(it.next()));
                if (root != null) {
                    MyDefaultMutableTreeNode nodeToLoad = constructTree(root, false, list);
                    if (nodeToLoad.getColor() == MyDefaultMutableTreeNode.DIFF_NAME) {
                        treeList.add(0, nodeToLoad);
                    } else {
                        treeList.add(nodeToLoad);
                    }
                    nodeToLoad.setColor(MyDefaultMutableTreeNode.DEFAULT);
                }

            }
            for (int i = 0; i < treeList.size(); i++) {
                operationLoadTree(treeList.get(i));
                // System.out.println("TREE LOADED for "+treeList.get(i).getName());
            }
            if (defaultSelected != null && defaultSelected.length > 0) {
                for (int i = 0; i < defaultSelected.length; i++) {
                    defaultSelectedTreeNodes.add(nodesIndex.get(defaultSelected[i].intValue()));
                }
            }
            if (addchild) {
                nodesIndex.clear();
                convertToAddTree(operationGetRoot(), false);
            }
            for (int i = 0; i < defaultSelectedTreeNodes.size(); i++) {
                MyDefaultMutableTreeNode nodeToSelect = defaultSelectedTreeNodes.get(i);
                if (addchild) {
                    if (nodeToSelect != null && nodeToSelect.getChildCount() > 0) {
                        nodeToSelect.getChildAt(nodeToSelect.getChildCount() - 1).setCheckBoxSelectable(false);
                        nodeToSelect.getChildAt(nodeToSelect.getChildCount() - 1).setSelectedSingle(true);
                    }
                } else {
                    nodeToSelect.setCheckBoxSelectable(false);
                    nodeToSelect.setSelectedSingle(true);
                }
            }
            operationRefreshTree();
            Iterator<Integer> ite = nodesIndex.keySet().iterator();
            while (ite.hasNext()) {
                operationExpandPath(nodesIndex.get(ite.next().intValue()));
            }
            selectedNo = defaultSelected.length;
            operationChangeSelectedNumber("" + selectedNo);
            setModelProperty(SHOW_SELECTER,
                    true);
        }
    }
    private HashMap<Integer, MyDefaultMutableTreeNode> nodesIndex = new HashMap<Integer, MyDefaultMutableTreeNode>();

    private MyDefaultMutableTreeNode constructTree(TestDesignTreeNode node, boolean selectable, LinkedList<TestDesignTreeNode> selectableList) {
        HashMap<Integer, LinkedList<TestDesignTreeNode>> nodesPath = new HashMap<Integer, LinkedList<TestDesignTreeNode>>();
        for (int i = 0; i < selectableList.size(); i++) {
            TestDesignTreeNode tmpNode = selectableList.get(i);
            LinkedList<TestDesignTreeNode> nodePath = new LinkedList<TestDesignTreeNode>();
            nodePath.add(tmpNode);
            boolean found = true;
            while (node != tmpNode) {
                if (tmpNode != null) {
                    tmpNode = tmpNode.getParent();
                    if (tmpNode != null) {
                        nodePath.add(tmpNode);
                    } else {
                        found = false;
                        break;
                    }
                } else {
                    found = false;
                    break;
                }
            }
            if (found) {
                nodesPath.put(nodesPath.size(), nodePath);
            }
        }
        HashMap<Integer, MyDefaultMutableTreeNode> insertedNodeList = new HashMap<Integer, MyDefaultMutableTreeNode>();
        MyDefaultMutableTreeNode result = new MyDefaultMutableTreeNode(node, selectable);
        insertedNodeList.put(node.getId(), result);
        for (int i = 0; i < nodesPath.size(); i++) {

            LinkedList<TestDesignTreeNode> nodePath = nodesPath.get(i);
            MyDefaultMutableTreeNode current = result;
            for (int j = nodePath.size() - 2; j >= 0; j--) {
                MyDefaultMutableTreeNode tmp;
                Integer nodeId = nodePath.get(j).getId();
                TestDesignTreeNode nodeVal = nodePath.get(j);
                if (!insertedNodeList.containsKey(nodeId)) {
                    TestDesignTreeNode parent = nodeVal.getParent();
                    LinkedList<TestDesignTreeNode> leafNodes;
                    if (parent != null) {
                        current = insertedNodeList.get(parent.getId());
                        leafNodes = parent.getChildrenAsList();
                    } else {
                        leafNodes = new LinkedList<TestDesignTreeNode>();
                        leafNodes.add(nodeVal);
                    }
                    for (int l = 0; l < leafNodes.size(); l++) {
                        TestDesignTreeNode theNode = leafNodes.get(l);
                        boolean contains = false;
                        int index = 0;
                        for (index = 0; index < selectableList.size(); index++) {
                            if (selectableList.get(index).getId() == theNode.getId()) {
                                contains = true;
                                break;
                            }
                        }
                        if (contains) {
                            tmp = new MyDefaultMutableTreeNode(theNode, true);
                            if (!theNode.isClone()) {
                                tmp.setColor(MyDefaultMutableTreeNode.DIFF_NAME);
                                result.setColor(MyDefaultMutableTreeNode.DIFF_NAME);
                            } else {
                                if (result.getColor() != MyDefaultMutableTreeNode.DIFF_NAME) {
                                    result.setColor(MyDefaultMutableTreeNode.DIFF_PROPERTY);
                                }
                                tmp.setColor(MyDefaultMutableTreeNode.DIFF_PROPERTY);
                            }
                            if (addChildMode) {
                                TestDesignTreeNode[] children = theNode.getChildren();
                                for (int k = 0; k < children.length; k++) {
                                    MyDefaultMutableTreeNode child = new MyDefaultMutableTreeNode(children[k], false);
                                    tmp.add(child);
                                }
                            }
                            nodesIndex.put(index, tmp);
                        } else {
                            tmp = new MyDefaultMutableTreeNode(theNode, false);
                        }
                        current.add(tmp);
                        insertedNodeList.put(theNode.getId(), tmp);
                    }
                }
            }
        }
        return result;
    }

    private MyDefaultMutableTreeNode constructTree(DynamicMapDesignTreeNode node, boolean selectable, LinkedList<DynamicMapDesignTreeNode> selectableList) {
        HashMap<Integer, LinkedList<DynamicMapDesignTreeNode>> nodesPath = new HashMap<Integer, LinkedList<DynamicMapDesignTreeNode>>();
        for (int i = 0; i < selectableList.size(); i++) {
            DynamicMapDesignTreeNode tmpNode = selectableList.get(i);
            LinkedList<DynamicMapDesignTreeNode> nodePath = new LinkedList<DynamicMapDesignTreeNode>();
            nodePath.add(tmpNode);
            boolean found = true;
            while (node != tmpNode) {
                if (tmpNode != null) {
                    tmpNode = tmpNode.getParent();
                    if (tmpNode != null) {
                        nodePath.add(tmpNode);
                    } else {
                        found = false;
                        break;
                    }
                } else {
                    found = false;
                    break;
                }
            }
            if (found) {
                nodesPath.put(nodesPath.size(), nodePath);
            }
        }
        HashMap<Integer, MyDefaultMutableTreeNode> insertedNodeList = new HashMap<Integer, MyDefaultMutableTreeNode>();
        MyDefaultMutableTreeNode result = new MyDefaultMutableTreeNode(node, selectable);
        insertedNodeList.put(node.getId(), result);
        for (int i = 0; i < nodesPath.size(); i++) {
            LinkedList<DynamicMapDesignTreeNode> nodePath = nodesPath.get(i);
            MyDefaultMutableTreeNode current = result;
            for (int j = nodePath.size() - 2; j >= 0; j--) {
                MyDefaultMutableTreeNode tmp;
                Integer nodeId = nodePath.get(j).getId();
                DynamicMapDesignTreeNode nodeVal = nodePath.get(j);
                if (!insertedNodeList.containsKey(nodeId)) {
                    DynamicMapDesignTreeNode parent = nodeVal.getParent();
                    LinkedList<DynamicMapDesignTreeNode> leafNodes;
                    if (parent != null) {
                        current = insertedNodeList.get(parent.getId());
                        leafNodes = parent.getChildrenAsList();
                    } else {
                        leafNodes = new LinkedList<DynamicMapDesignTreeNode>();
                        leafNodes.add(nodeVal);
                    }
                    for (int l = 0; l < leafNodes.size(); l++) {
                        DynamicMapDesignTreeNode theNode = leafNodes.get(l);
                        boolean contains = false;
                        int index = 0;
                        for (index = 0; index < selectableList.size(); index++) {
                            if (selectableList.get(index).getId() == theNode.getId()) {
                                contains = true;
                                break;
                            }
                        }
                        if (contains) {
                            tmp = new MyDefaultMutableTreeNode(theNode, true);
                            if (!theNode.isClone()) {
                                tmp.setColor(MyDefaultMutableTreeNode.DIFF_NAME);
                                result.setColor(MyDefaultMutableTreeNode.DIFF_NAME);
                            } else {
                                if (result.getColor() != MyDefaultMutableTreeNode.DIFF_NAME) {
                                    result.setColor(MyDefaultMutableTreeNode.DIFF_PROPERTY);
                                }
                                tmp.setColor(MyDefaultMutableTreeNode.DIFF_PROPERTY);
                            }
                            if (addChildMode) {
                                DynamicMapDesignTreeNode[] children = theNode.getChildren();
                                for (int k = 0; k < children.length; k++) {
                                    MyDefaultMutableTreeNode child = new MyDefaultMutableTreeNode(children[k], false);
                                    tmp.add(child);
                                }
                            }
                            nodesIndex.put(index, tmp);
                        } else {
                            tmp = new MyDefaultMutableTreeNode(theNode, false);
                        }
                        current.add(tmp);
                        insertedNodeList.put(theNode.getId(), tmp);
                    }
                }
            }
        }
        return result;
        /*   MyDefaultMutableTreeNode result = new MyDefaultMutableTreeNode(node, selectable);
        for (int i = 0; i < node.getChildrenLinkedList().size(); i++) {
        DynamicMapDesignTreeNode childNode = (DynamicMapDesignTreeNode) node.getChildrenLinkedList().get(i);
        boolean childSelectable = false;
        int k = 0;
        for (; selectableList != null && !childSelectable && k < selectableList.size(); k++) {
        if (selectableList.get(k).getId() == childNode.getId()) {
        selectableList.remove(k);
        if (k == selectableList.size()) {
        selectableList.add(childNode);
        } else {
        selectableList.add(k, childNode);
        }
        childSelectable = true;
        }
        }
        MyDefaultMutableTreeNode child = constructTree(childNode, childSelectable, selectableList);
        if (childSelectable) {
        nodesIndex.put(k - 1, child);
        child.setColor(MyDefaultMutableTreeNode.DIFF_PROPERTY);
        }
        result.add(child);
        }
        return result;*/
    }

    private void addIfUnique(TestDesignTreeNode suite, HashMap<Integer, LinkedList> hashMap) {
        if (hashMap.containsKey(suite.getId())) {
            return;
        }
        hashMap.put(suite.getId(), new LinkedList());
    }

    private void addIfUnique(DynamicMapDesignTreeNode suite, HashMap<Integer, LinkedList> hashMap) {
        if (hashMap.containsKey(suite.getId())) {
            return;
        }
        hashMap.put(suite.getId(), new LinkedList());
    }

    public MyDefaultMutableTreeNode getSelectedMutableTreeNode() {
        return (MyDefaultMutableTreeNode) getModelProperty(SELECT_DESIGN_PART);
    }
}
